/**
 *
 */
package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.DriverHelppayRecordMapper;
import com.ybkj.daijia.server.mapper.DriverPaymentsRecordMapper;
import com.ybkj.daijia.server.model.DriverPaymentsRecord;
import com.ybkj.daijia.server.model.HelpPayRecord;
import com.ybkj.daijia.server.service.DriverPaymentsRecordService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Shine
 *
 */
@Service
public class DefaultDriverPaymentsRecordService implements DriverPaymentsRecordService {

    public static final String NAME2 = "recordType";
    public static final String NAME3 = "recordTypeStr";
    public static final String NAME4 = "operateType";
    public static final String NAME5 = "rcType";
    public static final String NAME6 = "money";
    private static final String NAME = "happend";
    @Autowired
    private DriverPaymentsRecordMapper driverPaymentsRecordMapper;
    @Autowired
    private DriverHelppayRecordMapper helppayRecordMapper;

    @Transactional(rollbackFor = Exception.class)
    public DriverPaymentsRecord insertRecord(DriverPaymentsRecord driverPaymentsRecord) {

        driverPaymentsRecordMapper.insertRecord(driverPaymentsRecord);

        return driverPaymentsRecord;
    }

    @Override
    public DriverPaymentsRecord findOne(Long id) {

        return driverPaymentsRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<DriverPaymentsRecord> listIndexPayments(Long driverId, Date start, Date end) {

        return driverPaymentsRecordMapper.listIndexPayments(driverId, start, end);
    }

    @Override
    public BigDecimal sumInComeByEmployWithDate(Long driverId, Date start, Date end) {
        BigDecimal Income = driverPaymentsRecordMapper
            .sumInComeByEmployWithDate(driverId, start, end);
        if (null == Income) {
            Income = BigDecimal.ZERO;
        }
        return Income;
    }

    @Override
    public Page<DriverPaymentsRecord> queryByEmploy(Long driverId, String recordType,
        Integer rcType, Date start, Date end, Integer page, Integer limit) {

        Pageable pageable = new PageRequest(page, limit);

        List<DriverPaymentsRecord> content = driverPaymentsRecordMapper
            .selectByEmploy(driverId, recordType, rcType, start, end, page * limit, limit);
        long total = driverPaymentsRecordMapper
            .countByEmploy(driverId, recordType, rcType, start, end);

        return new PageImpl<DriverPaymentsRecord>(content, pageable, total);
    }

    @Override
    public Page<DriverPaymentsRecord> queryByEmployRecordTypes(Long driverId, Integer rcType,
        Date start, Date end, Integer page, Integer limit, String... recordTypes) {

        Pageable pageable = new PageRequest(page, limit);

        List<DriverPaymentsRecord> content = driverPaymentsRecordMapper
            .selectByEmployAndRecordTypeCollections(driverId, rcType, start, end, page * limit,
                limit, recordTypes);
        long total = driverPaymentsRecordMapper
            .countByEmployAndRecordTypeCollections(driverId, rcType, start, end, recordTypes);

        return new PageImpl<DriverPaymentsRecord>(content, pageable, total);
    }

    public Page<Map<String, Object>> queryByEmply2(Long driverId, String recordType, Integer rcType,
        Date start, Date end, Integer page, Integer limit) {
        List<Map<String, Object>> list1 = driverPaymentsRecordMapper
            .selectByEmploy2(driverId, null, null, start, end, null, null);
        List<Map<String, Object>> list2 = helppayRecordMapper
            .getListByDriverId2(start, end, driverId);

        list1.addAll(list2);
        Collections.sort(list1, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Date happen1 = (Date) o1.get(NAME);
                Date happen2 = (Date) o2.get(NAME);
                return happen2.compareTo(happen1);

            }
        });
        int startIndex = limit * page;
        int endIndex = startIndex + limit;
        List<Map<String, Object>> re = new ArrayList<>();
        for (int i = startIndex; i < endIndex && i < list1.size(); i++) {
            Map<String, Object> obj = list1.get(i);
            re.add(obj);
            if (obj.containsKey(NAME5)) {
                obj.put(NAME3, DriverPaymentsRecord.getRecordTypeStr(obj.get(NAME2).toString()));
            } else {
                Integer operateType = Integer.valueOf(obj.get(NAME4).toString());
                obj.put(NAME5, HelpPayRecord.getRcType(operateType));
                obj.put(NAME3, HelpPayRecord.getRecordTypeStr(operateType));
            }
        }
        Pageable pageable = new PageRequest(page, limit);
        return new PageImpl<Map<String, Object>>(re, pageable, (long) list1.size());
    }

    @Override
    public List<DriverPaymentsRecord> listByEmploy(Long driverId, String recordType, Integer rcType,
        Date start, Date end) {

        List<DriverPaymentsRecord> content = driverPaymentsRecordMapper
            .selectByEmploy(driverId, recordType, rcType, start, end, null, null);

        return content;
    }

    @Override
    public BigDecimal sumRebateByEmployWithDate(Long driverId, Date start, Date end) {
        BigDecimal Income = driverPaymentsRecordMapper
            .sumRebateByEmployWithDate(driverId, start, end);
        if (null == Income) {
            Income = BigDecimal.ZERO;
        }
        return Income;
    }

    @Override
    public DriverPaymentsRecord findOrderRecharge(Long driverId, Long orderId, Integer orderType) {
        List<DriverPaymentsRecord> driverPaymentsRecords = driverPaymentsRecordMapper
            .findOrderRecharge(driverId, orderId, orderType);
        if (!CollectionUtils.isEmpty(driverPaymentsRecords)) {
            return driverPaymentsRecords.get(0);
        }
        return null;
    }
}
	
	
